package com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.Fragment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bw.liuyushan.mvp_lib.View.BaseFragment;
import com.bw.liuyushan.zhuangao4_mvptest1.Adapter.VideoPageManager;
import com.bw.liuyushan.zhuangao4_mvptest1.Adapter.ViedoPageAdapter;
import com.bw.liuyushan.zhuangao4_mvptest1.Entity.ViedoBean;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Contrack.ICTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Model.BaseModelTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.zhuangao4_mvptest1.MVP.View.ui.MyStand;
import com.bw.liuyushan.zhuangao4_mvptest1.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

/**
 * ZhuanGao4_MVPTest1
 * file name is : Fragment_Main3
 * created by Ender on 2021/8/11 14:47.
 * author : Liuyushan
 * Describe:Fragment类 也就是V层 向P层要数据 进行显示
 */
public class Fragment_Main3 extends BaseFragment<BasePresenterTest> implements ICTest.C_ViewTest, OnRefreshLoadMoreListener {
    private SmartRefreshLayout smVideoPage;
    private RecyclerView rvVideoPage;
    int currentPage = 1;//页数
    boolean isPage;
    ViedoPageAdapter adapter;//适配器


    @Override
    public int bindLayout() {
        return R.layout.fragment_main3;
    }

    @Override
    public void initView() {
        //防止内存泄露
        ip = new BasePresenterTest(new BaseModelTest(), this);

        smVideoPage = (SmartRefreshLayout) findViewById(R.id.sm_VideoPage);
        rvVideoPage = (RecyclerView) findViewById(R.id.rv_VideoPage);

        smVideoPage.setOnRefreshListener(this);
        smVideoPage.setOnRefreshLoadMoreListener(this);
        //设置布局管理器
        rvVideoPage.setLayoutManager(new VideoPageManager(getContext()));
        //添加滚动监听器事件
        rvVideoPage.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE://SCROLL_STATE_IDLE事件结束时调用->达成滑到下一个自动播放效果
                        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                        MyStand player = layoutManager.getChildAt(0).findViewById(R.id.Stand_VideoPage);
                        player.startPlayLogic();
                        break;
                }
            }
        });


    }

    @Override
    public void initDate() {
        ip.mpDate("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }

    @Override
    public void ShowDate(ViedoBean viedoBean) {
        List<ViedoBean.DataBean> data = viedoBean.getData();
        initAdapter(data);
    }

    private void initAdapter(List<ViedoBean.DataBean> data) {
        if (adapter == null) {
            adapter = new ViedoPageAdapter(data);
            rvVideoPage.setAdapter(adapter);//设置适配器
        } else {
            if (isPage) {
                adapter.getData().clear();
            }
            adapter.getData().addAll(data);
            adapter.notifyDataSetChanged();
        }
        smVideoPage.finishRefresh();
        smVideoPage.finishLoadMore();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isPage=false;
        currentPage++;
        ip.mpDate("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isPage=true;
        currentPage=1;
        ip.mpDate("zytestapi/video/findVideos?pageSize=10&currentPage=" + currentPage);
    }
}
